﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

/// <summary>
/// 显示个人或小组网盘页面
/// 验证登录
/// 如果personal为空，验证是否进入小组页面
/// 可选GET参数：
/// personal —— 指示是否只显示个人网盘
/// </summary>
public partial class filestorage : System.Web.UI.Page
{
    private DataTable personalFileList = new DataTable();
    private DataTable sharedFileList = new DataTable();
    private DataTable editionManageFileList = new DataTable();
    private string choice = "";

    /// <summary>
    /// 执行验证
    /// 初始化页面
    /// 读取文件列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["userID"] == null)
        {
            Response.Redirect("notLogin.html", true);
            return;
        }
        if (Request.QueryString["personal"] == null)
        {
            if (Request.Cookies["groupID"] == null)
            {
                Response.Redirect("welcome.aspx", true);
                return;
            }
        }
        //因为通过GET参数personal的有无来判断是否为个人页面，所以不再判断GET参数是否齐全

        if (IsPostBack)
        {
            return;
        }

        string userID = Request.Cookies["userID"].Value;
        //string personal = Request.QueryString["personal"];
        if (Request.QueryString["personal"] == null)
        {
            //非个人文件页面
            //获取个人文件列表
            personalFileList.Columns.Add("name");
            personalFileList.Columns.Add("size");
            personalFileList.Columns.Add("time");
            string path_personal = "~/filestorage/" + userID + "_PS_ROOT";
            path_personal = Server.MapPath(path_personal);
            if (Directory.Exists(path_personal))
            {
                FileInfo[] arrFile = new DirectoryInfo(path_personal).GetFiles();
                foreach (FileInfo fi in arrFile)
                {
                    DataRow row = personalFileList.NewRow();
                    row["name"] = fi.Name;
                    row["size"] = fi.Length.ToString();
                    row["time"] = fi.LastWriteTime.ToString();
                    personalFileList.Rows.Add(row);
                }
                PersoanlFileGridView.DataSource = personalFileList;
                PersoanlFileGridView.DataBind();
            }

            //获取共享文件列表
            sharedFileList.Columns.Add("name");
            sharedFileList.Columns.Add("size");
            sharedFileList.Columns.Add("time");
            string path_shared = "~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/SharedFile";
            path_shared = Server.MapPath(path_shared);
            if (Directory.Exists(path_shared))
            {
                FileInfo[] garrFile = new DirectoryInfo(path_shared).GetFiles();
                foreach (FileInfo fi in garrFile)
                {
                    DataRow row = sharedFileList.NewRow();
                    row["name"] = fi.Name;
                    row["size"] = fi.Length.ToString();
                    row["time"] = fi.LastWriteTime.ToString();
                    sharedFileList.Rows.Add(row);
                }
                SharedFileGridView.DataSource = sharedFileList;
                SharedFileGridView.DataBind();
            }

            //获取版本管理文件列表
            editionManageFileList.Columns.Add("name");
            string path_editionManage = "~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/EM";
            path_editionManage = Server.MapPath(path_editionManage);
            if (Directory.Exists(path_editionManage))
            {
                DirectoryInfo[] darrFile = new DirectoryInfo(path_editionManage).GetDirectories();
                foreach (DirectoryInfo fi in darrFile)
                {
                    DataRow row = editionManageFileList.NewRow();
                    row["name"] = fi.Name;
                    editionManageFileList.Rows.Add(row);
                }
                EditionManageDataList.DataSource = editionManageFileList;
                EditionManageDataList.DataBind();
            }
        }
        else if (Request.QueryString["personal"] == "1")
        {
            //个人文件页面
            //隐藏小组信息模块
            SharedFileLabel.Visible = false;
            EditionManageLabel.Visible = false;
            SharedFileUpload.Visible = false;
            EditionManageFileUpload.Visible = false;
            SharedFileGridView.Visible = false;
            EditionManageDataList.Visible = false;
            SharedFileUploadButton.Visible = false;
            EditionManageUploadButton.Visible = false;

            //获取个人文件列表
            personalFileList.Columns.Add("name");
            personalFileList.Columns.Add("size");
            personalFileList.Columns.Add("time");
            string path_personal = "~/filestorage/" + userID + "_PS_ROOT";
            path_personal = Server.MapPath(path_personal);
            if (Directory.Exists(path_personal))
            {
                FileInfo[] arrFile = new DirectoryInfo(path_personal).GetFiles();
                foreach (FileInfo fi in arrFile)
                {
                    DataRow row = personalFileList.NewRow();
                    row["name"] = fi.Name;
                    row["size"] = fi.Length.ToString();
                    row["time"] = fi.LastWriteTime.ToString();
                    personalFileList.Rows.Add(row);
                }
            }
            PersoanlFileGridView.DataSource = personalFileList;
            PersoanlFileGridView.DataBind();
        }
        else
        {
            Response.Redirect("noGETParameter.html", true);
            return;
        }
    }

    /// <summary>
    /// 个人文件上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PersonalFileUploadButton_Click(object sender, EventArgs e)
    {
        string FilePath = "";
        try
        {
            if (!string.IsNullOrEmpty(PersonalFileUpload.FileName))
            {
                if (PersonalFileUpload.PostedFile.ContentLength <= 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('上传图片文件为空，请重新选择文件!')</script>");
                    return;
                }
                if (PersonalFileUpload.HasFile)
                {
                    if (PersonalFileUpload.PostedFile.ContentLength > 4196304*250)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件大小超过1G，请选择更小的图片上传!')</script>");
                        return;
                    }
                    else
                    {
                        FilePath = Server.MapPath("~/filestorage/" + Request.Cookies["userID"].Value + "_PS_ROOT");
                    }

                    Tools.upload(this, FilePath, PersonalFileUpload);
                    Response.Redirect("filestorage.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('所选路径不存在!')</script>");
                    return;
                }
            }
        }
        catch (Exception ex_toobig)
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('" + ex_toobig.Message + ex_toobig.StackTrace + "!')</script>");
            return;
        }
    }

    /// <summary>
    /// 小组共享文件上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SharedFileUploadButton_Click(object sender, EventArgs e)
    {
        string FilePath = "";
        try
        {
            if (!string.IsNullOrEmpty(SharedFileUpload.FileName))
            {
                if (SharedFileUpload.PostedFile.ContentLength <= 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('上传图片文件为空，请重新选择文件!')</script>");
                    return;
                }
                if (SharedFileUpload.HasFile)
                {
                    if (SharedFileUpload.PostedFile.ContentLength > 4196304*250)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件大小超过1G，请选择更小的图片上传!')</script>");
                        return;
                    }
                    else
                    {
                        FilePath = Server.MapPath("~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/SharedFile");
                    }

                    Tools.upload(this, FilePath, SharedFileUpload);
                    Response.Redirect("filestorage.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('所选路径不存在!')</script>");
                    return;
                }
            }
        }
        catch (Exception ex_toobig)
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('" + ex_toobig.Message + ex_toobig.StackTrace + "!')</script>");
            return;
        }
    }

    /// <summary>
    /// 上传版本管理文件，添加母版
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void EditionManageUploadButton_Click(object sender, EventArgs e)
    {
        string FilePath = "";
        try
        {
            if (!string.IsNullOrEmpty(EditionManageFileUpload.FileName))
            {
                if (EditionManageFileUpload.PostedFile.ContentLength <= 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('上传图片文件为空，请重新选择文件!')</script>");
                    return;
                }
                if (EditionManageFileUpload.HasFile)
                {
                    if (EditionManageFileUpload.PostedFile.ContentLength > 4196304*250)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件大小超过1G，请选择更小的图片上传!')</script>");
                        return;
                    }
                    else
                    {
                        FilePath = Server.MapPath("~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/EM/" + EditionManageFileUpload.FileName);
                    }

                    Tools.upload(this, FilePath, EditionManageFileUpload);
                    Response.Redirect("filestorage.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('所选路径不存在!')</script>");
                    return;
                }
            }
        }
        catch (Exception ex_toobig)
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('" + ex_toobig.Message + ex_toobig.StackTrace + "!')</script>");
            return;
        }
    }

    /// <summary>
    /// 个人文件删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void deletePersonalFile(object sender, GridViewDeleteEventArgs e)
    {
        String filename = PersoanlFileGridView.Rows[e.RowIndex].Cells[0].Text;
        string path = "~/filestorage/" + Request.Cookies["userID"].Value + "_PS_ROOT/" + filename;
        path = Server.MapPath(path);
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件已经不存在了!')</script>");
            return;
        }
        Response.Redirect("filestorage.aspx");
    }

    /// <summary>
    /// 小组共享文件删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void deleteSharedFile(object sender, GridViewDeleteEventArgs e)
    {
        String filename = PersoanlFileGridView.Rows[e.RowIndex].Cells[0].Text;
        string path = "~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/SharedFile" + filename;
        path = Server.MapPath(path);
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件已经不存在了!')</script>");
            return;
        }
        Response.Redirect("filestorage.aspx");
    }
}